package com.weipeep.sys.controller;


import java.util.Arrays;
import java.util.List;

import com.google.common.collect.Lists;
import com.weipeep.sys.vo.ValueTextVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.weipeep.sys.domain.ClassTypeDO;
import com.weipeep.sys.service.ClassTypeService;
import com.weipeep.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2018-04-19 11:59:41 | wangyun</small>
 */
@Controller
@RequestMapping("/sys/classType")
public class ClassTypeController {
	@Autowired
	private ClassTypeService classTypeService;
	
	@GetMapping()
	@RequiresPermissions("sys:classType:classType")
	String ClassType(){
	    return "sys/classType/classType";
	}
	
	@ResponseBody
	@GetMapping("/page")
	@RequiresPermissions("sys:classType:classType")
	public Result<Page<ClassTypeDO>> page(Integer pageNumber, Integer pageSize, ClassTypeDO classTypeDTO){
		// 查询列表数据
        Page<ClassTypeDO> page = new Page<>(pageNumber, pageSize);
        
        Wrapper<ClassTypeDO> wrapper = new EntityWrapper<ClassTypeDO>(classTypeDTO);
        page = classTypeService.selectPage(page, wrapper);
        int total = classTypeService.selectCount(wrapper);
        page.setTotal(total);
        return Result.ok(page);
	}
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("sys:classType:classType")
	public Result<List<ValueTextVo>> list(ClassTypeDO classTypeDTO){
		// 查询列表数据
		Wrapper<ClassTypeDO> wrapper = new EntityWrapper<ClassTypeDO>(classTypeDTO);
		List<ClassTypeDO> classTypeDOList =  classTypeService.selectList(wrapper);
		List<ValueTextVo> valueTextVoList  = Lists.newArrayList();
		classTypeDOList.stream().forEach(classTypeDO -> {
			ValueTextVo valueTextVo = new ValueTextVo();
			valueTextVo.setValue(classTypeDO.getId());
			valueTextVo.setTitle(classTypeDO.getTitle());
			valueTextVoList.add(valueTextVo);
		});
		return Result.ok(valueTextVoList);
	}
	@GetMapping("/add")
	@RequiresPermissions("sys:classType:add")
	String add(){
	    return "sys/classType/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("sys:classType:edit")
	String edit(@PathVariable("id") Long id,Model model){
		ClassTypeDO classType = classTypeService.selectById(id);
		model.addAttribute("classType", classType);
	    return "sys/classType/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("sys:classType:add")
	public Result<String> save( ClassTypeDO classType){
		if(classTypeService.insert(classType)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("sys:classType:edit")
	public Result<String>  update( ClassTypeDO classType){
		classTypeService.updateById(classType);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("sys:classType:remove")
	public Result<String>  remove( Long id){
		if(classTypeService.deleteById(id)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("sys:classType:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		classTypeService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
